/**
 * Created with hong.rong
 * Date:2020/07/17
 */

import React, { useState, Fragment } from 'react'
import { isFunction } from 'lodash'
import { Modal, Form } from 'antd'
import SubmitForm from './SubmitForm'

const TYPE = {
  CREATE: {
    LABEL: '发布团购',
    VALUE: 'CREATE',
  },
  EDIT: {
    LABEL: '新增团购',
    VALUE: 'EDIT',
  },
  VIEW: {
    LABEL: '查看团购',
    VALUE: 'VIEW',
  },
}

export default function Modify({ children, refreshList }) {
  const [visible, setVisible] = useState(false)
  const [confirmLoading, setConfirmLoading] = useState(false)
  const [modalType, setModalType] = useState(null)
  const [form] = Form.useForm()

  /**
   * 提交
   */
  const handleOk = () => {
    form.validateFields().then(async values => {
      console.log(values, 'ok')
      // delete values.position
      // delete values.serviceDate
      // setConfirmLoading(true)
      // const { data = {} } = await carryModify(values)
      // if (data && data.code === 0) {
      //   message.success('保存成功')
      //   handleCancel()
      //   isFunction(refreshList) && refreshList()
      // } else {
      //   handleCancel()
      // }
    })
  }
  /**
   * 显示弹框
   * @type {*|Function}
   */
  const handleShow = (modalType, current) => {
    if (modalType === TYPE.EDIT.VALUE || modalType === TYPE.VIEW.VALUE) {
      form.setFieldsValue(current)
    }
    setModalType(modalType)
    setVisible(true)
  }
  /**
   * 取消弹框
   * @type {*|Function}
   */
  const handleCancel = () => {
    setVisible(false)
    setConfirmLoading(false)
    setModalType(null)
    form.resetFields()
  }

  return (
    <Fragment>
      {isFunction(children) && children(handleShow)}
      <Modal
        centered
        width={1000}
        destroyOnClose
        title={(TYPE[modalType] || {}).LABEL}
        visible={visible}
        confirmLoading={confirmLoading}
        onOk={handleOk}
        onCancel={handleCancel}
      >
        <SubmitForm form={form} />
      </Modal>
    </Fragment>
  )
}
Modify.TYPE = TYPE
